package com.stericson.actionmenu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MenuService
{
	
	private Activity activity;
	private LinearLayout menu;
	
	public MenuService(Activity activity)
	{
		this.activity = activity;
		menu = (LinearLayout) activity.findViewById(R.id.menuContainer);
	}
	
	private LinearLayout addMenuItem(int menuText, int menuImage)
	{		
		return addMenuItem(activity.getString(menuText), menuImage);
	}

	private LinearLayout addMenuItem(String menuText, int menuImage)
	{
    	LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout menuItem = (LinearLayout) inflater.inflate(R.layout.menu_item, null);
		
		ImageView item_image = (ImageView) menuItem.findViewById(R.id.menuitem_image);
		TextView item_text = (TextView) menuItem.findViewById(R.id.menuitem_text);
		
		item_image.setImageResource(menuImage);
		item_text.setText(menuText);
		
		return menuItem;
	}

	public boolean isVisible()
	{
		return menu != null && menu.getVisibility() == View.VISIBLE;
	}
	
	public void hideMenu()
	{
		
		if (isVisible())
		{
			//You can change the anmation here, I included some others that I made.
			Animation anim = AnimationUtils.loadAnimation(menu.getContext(), R.anim.exit_slideout);
			menu.startAnimation(anim);
			anim.setAnimationListener(new AnimationListener() {
	
				@Override
	            public void onAnimationEnd(Animation arg0)
	            {
					menu.removeAllViews();
					menu.setVisibility(View.GONE);
				}
	
				@Override
	            public void onAnimationRepeat(Animation arg0) {}
	
				@Override
	            public void onAnimationStart(Animation arg0) {}
				
			});
		}
	}
	
	private void showMenu()
	{
		menu.setVisibility(View.VISIBLE);
		//You can change the anmation here, I included some others that I made.
		Animation anim = AnimationUtils.loadAnimation(menu.getContext(), R.anim.enter_slidein);
		menu.startAnimation(anim);		
	}
	
	//You can create as many menus as you would like.
	public void mainMenu()
	{
		
		if (!isVisible())
		{
			addMenuItem1();

			addMenuItem2();
			
			showMenu();
		}
		else
			hideMenu();
	}		

	public void addMenuItem2()
    {
	    LinearLayout about = addMenuItem("Menu Item 1", R.drawable.icon);
	    about.setOnClickListener(new OnClickListener() {

	    	@Override
	        public void onClick(View arg0)
	        {
	    		hideMenu();
	    		//Do some work here :)
	        }
	    });

	    menu.addView(about);
    }

	public void addMenuItem1()
    {
	    LinearLayout home = addMenuItem("Menu Item 2", R.drawable.icon);
	    home.setOnClickListener(new OnClickListener() {

	    	@Override
	        public void onClick(View arg0)
	        {
	    		hideMenu();
	    		//Do some work here :)
	        }
	    	
	    });

	    menu.addView(home);
    }
}
